#  Copyright (C) 2010 Luca Piccinelli
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

'''
Created on Jul 28, 2011

@author: picci
'''

from pipeit import AbsPipeStepDescrBuilder,\
    Configuration,                        \
    GeneratorFactoriesPool_get_instance,  \
    PipelineDescriptor
from pypelab import globals

class GUIPipeStepDescrBuilder(AbsPipeStepDescrBuilder):
    #****** GUIPipeStepDescrBuilder *************************************
    """ Builds a list of PipeStepDesctiptor reading the pipeWorkArea  as
        source """                
    #********************************************************************
    
    def __init__(self, pipeWorkArea):
        super().__init__()
        self.pipeWorkArea = pipeWorkArea 
        
    def build(self, descriptors):
        
        if self.pipeWorkArea.subWindowList() == None or len(self.pipeWorkArea.subWindowList()) == 0: return []
        pipeDescr = PipelineDescriptor(self.pipeWorkArea.id)
        pipeDescr.thisown = 0
                
        for subWnd in self.pipeWorkArea.subWindowList():
            wid = subWnd.widget()
            descr = wid.pipeStepDescriptor 
            
            wid = subWnd.widget()
            for cLabel in wid.criteria_labels:
                descr.set_criteria(cLabel.key, str(cLabel.buddy().value()) )
            
            self.__build_conf(wid.in_labels,  wid, 'IN')
            self.__build_conf(wid.out_labels, wid, 'OUT')
            
            pipeDescr.add_step(descr)
        
        descriptors.push_back(pipeDescr)
                
    # PRIVATE METHOD
    def __build_conf(self, labels, wid, io):
        descr = wid.pipeStepDescriptor
        
        for label in labels:
            if not label.group: 
                raise RuntimeError("Undefined input for: {0} -> {1} ".format(descr.get_id(), label.text()))
            template = GeneratorFactoriesPool_get_instance().get_configuration_template(label.group.generator_id)
            conf = Configuration(template)
            conf.set_IOKey_parameter(template.param_at(0), label.key)
            for l in label.group.confValues():
                type = l.key.getType()
                if type == globals.INT:
                    val = 0
                    if l.buddy().value() != '': val = l.buddy().value()
                    conf.set_int_parameter( l.key, int(val) )
                elif type == globals.LONG or type == globals.MEMORYSZ:
                    val = 0
                    if l.buddy().value() != '': val = l.buddy().value()
                    conf.set_long_parameter( l.key, int(val) )
                elif type == globals.FLOAT or type == globals.DOUBLE:
                    val = 0
                    if l.buddy().value() != '': val = l.buddy().value()
                    conf.set_double_parameter( l.key, float(val) )
                elif type == globals.STRING:
                    conf.set_string_parameter( l.key, str(l.buddy().value()) )
                else:
                    raise RuntimeError("Unsupported type {0} for Configuration element {1}".format(type, l.key.get_key()))                
                
            if io == 'IN':
                descr.set_in_conf(label.key, label.group.generator_id, conf)
            else:
                descr.set_out_conf(label.key, label.group.generator_id, conf)
                    
                                                    
                   
        
         
        